package com.baizhi.controller;

import com.baizhi.entity.Department;
import com.baizhi.entity.Drug;
import com.baizhi.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DrugController {
    @Autowired
    private DrugService drugService;

    @RequestMapping("selectDrugByPage")
    @ResponseBody
    public Map selectDrugByPage(int page, int rows, String name){
        Map map=new HashMap();
        int total = drugService.selectTotal(name);
        map.put("total",total);
        List<Drug> list = drugService.selectDrugByPage(page, rows, name);
        map.put("rows",list);
        return map;
    }

    @RequestMapping("addDrug")
    @ResponseBody
    public Map addDrug(Drug drug) {
        Map map = new HashMap();

        try {
            drugService.insert(drug);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }

    @RequestMapping("updateDrug")
    @ResponseBody
    public Map updateDrug(Drug drug) {
        Map map = new HashMap();

        //System.out.println(drug);

        try {
            drugService.update(drug);
            map.put("isUpdate", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate", false);
        }
        return map;
    }

    @RequestMapping("multiDeleteDrug")
    @ResponseBody
    public Map multiDeleteDrug(int ids[]) {
        Map map = new HashMap();
        //System.out.println();
        try {
            drugService.delete(ids);
            map.put("isDelete", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

}
